<?php

class MemberAction extends Action {
	public function _initialize() {
		import('PassportService');
		$MemberModel = D('member');
	}
	/**
	 * 会员注册
	 * @return [type] [description]
	 */
	public function register() {
		if(IS_POST) {
			$username = I('post.username' , '' , 'safe_html_fiter');
			$password = I('post.password' , '' , 'safe_html_fiter');
			$nickname = I('post.nickname' , '' , 'safe_html_fiter');
			$retType  = I('post.reg_type' , '' , 'safe_html_fiter');
			$type     = 0;
			//根据username 获得对应的注册方式
			get_reg_username($username , $email , $mobile ,$type);

			//根据当前的注册方式和username 判断是否对应的注册关系
			if( ($ret = get_reg_type($retType , $type)) !== true) {
				$this->error($ret);
			}
			//
			$uid = UserCenterModel()->register($username , $nickname , $password , $email , $mobile ,$type);
			if($uid > 0) {

			}else{
				echo $uid;
				$this->error(showRegError($uid));
			}
		}
		$this->assign('regSwitch',array( 0 =>'username'));
		$this->assign('type', $aType == '' ? 'username' : $aType);
		$this->assign('step' ,'start');
		$this->display();
	}

	/**
	 * 会员登录
	 * @return [type] [description]
	 */
	public function login() {
		if(IS_POST) {

			$result = $this->doLogin();

			if($result === true) {
				$this->success("登录成功!");
			}else{
				switch ($result) {
					case -1:
						$this->error("用户不存在或者禁用!");
						break;
					case -2:
						$this->error("用户密码错误!");
						break;
					default:
						$this->error("未知错误!");
						break;
				}
			}
		}
		$this->display();
	}

	/**
	 * 处理会员登录
	 * @return [type] [description]
	 */
	public function doLogin() {
		$username = I('post.username', '' ,'trim');
		$password = I('post.password', '' ,'trim');
		$aVerify  = I('post.verify', '', 'op_t');

		//1.判空
		if(empty($username) || empty($password)) {
			$this->error('账号或密码不能为空!');
		}

		//检测验证码是否开启
		
		//根据username 获得对应的注册方式
		get_reg_username($username , $email , $mobile ,$type);

		//检查注册方式是否打开
		
		//调用注册中心登录接口
		$uid = UserCenterModel()->login($username, $password, $type);

		if($uid > 0 ) {
			return true;
		}else {
			return $uid;
		}

	}


	/**
	 * Ajax验证昵称是否符合要求
	 * @return [type] [description]
	 */
	public function checkNickname() {
		$nickname = I('post.nickname' , '' , 'trim');
		//1.判空
		if(empty($nickname)) {
			$this->error('昵称不能为空!');
		}

		//2.长度检查
		$length = mb_strlen($nickname , 'utf8');
		if($length < 4 || $length > 32) {
			$this->error('昵称长度在4-32之间');
		}

		//3.检查是否合法
		preg_match('/^(?!_|\s\')[A-Za-z0-9_\x80-\xff\s\']+$/', $nickname
			, $result);
        if (!$result) {
            $this->error('只允许中文、字母和数字和下划线！');
        }

		//4.唯一性检查
		// if($MemberModel->checkNickname($nickname) == false) {
		// 	$this->error('该昵称已经存在！');
		// }

		$this->success('验证成功');
	}

	/**
	 * Ajax验证账号是否符合要求
	 * @return [type] [description]
	 */
	public function checkAccount() {
		$account = I('post.account' , '' , 'trim');
		$retType  = I('post.type' , '' , 'trim');
		//1.判空
		if(empty($account)) {
			$this->error('账号不能为空!');
		}

		get_reg_username($account , $email ,$mobile ,$type);
		switch ($retType) {
			case 'username':
				//长度检查
				if(!$this->checkLength($account)) {
					$this->error('用户名长度在4-32之间');
				}

				//检查是否合法
                if (!UserCenterModel()->checkUsername($account)) {
                    $this->error('只允许字母和数字和下划线！');
                }

				//唯一性检查
				if(!UserCenterModel()->IsUniqueUsername($account)) {
					$this->error('该用户名已经存在！'); 
				}
				break;
			case 'email':
				# code...
				break;
			case 'mobile':
				# code...
				break;
			default:
				# code...
				break;
		}
		$this->success('验证成功');
	}

	/**
	 * 检测查长度是否合法
	 * @param  [type]  $value   [description]
	 * @param  integer $min     [description]
	 * @param  integer $max     [description]
	 * @param  string  $charset [description]
	 * @return [type]           [description]
	 */
	public function checkLength($value ,$min = 4 ,$max = 32,  $charset = "utf-8") {
		$length = mb_strlen($value, 'utf-8'); // 当前数据长度
	    if ($length < $min || $length > $max) {
	    	return false;
	    }
	    return true;
	}
}